package me.zogodo.china.chineseapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Toast;

import java.io.IOException;
import java.util.Stack;

public class MainActivity extends AppCompatActivity
{
    //region 成员变量
    WebView webview_now;
    MyWebView webview_baidufanyi;
    MyWebView webview_taobao;
    MyWebView webview_jingdong;
    long exitTime = 0;
    int in_main = 1;
    //region 几个url
    String url_file = "file:///android_asset/test.html";
    String url_zogodo = "http://www.zogodo.cn/";
    String url22 = "https://m.jd.com/";
    String url_taobao_so = "http://s.m.taobao.com/history.htm";
    String url_taobao_li = "http://h5.m.taobao.com/mlapp/olist.html?spm=a2141.7756461.2.2&tabCode=waitSend";
    String url_taobao_login = "https://login.m.taobao.com/login.htm";
    String url_baidu_fy = "https://fanyi.baidu.com/#auto/zh/";
    String url_jd_so = "https://so.m.jd.com/ware/search.action";
    //endregion
    String js_jd = "";
    String js_taobao = "";
    String css_taobao = "";
    String css_jd = " ";
    String css_baidu = "";
    //endregion

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);

        //region 初始化 js css
        try
        {
            js_jd = ActivityReadFile.readTextFileFromRaw(this, R.raw.js_jd);
            js_taobao = ActivityReadFile.readTextFileFromRaw(this, R.raw.js_taobao);
            css_taobao = ActivityReadFile.readTextFileFromRaw(this, R.raw.css_tmall);
            css_jd = ActivityReadFile.readTextFileFromRaw(this, R.raw.css_jd);
            css_baidu = ActivityReadFile.readTextFileFromRaw(this, R.raw.css_baidu);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        //endregion

        webview_baidufanyi = new MyWebView(this, new Stack<MyWebView>());
        webview_baidufanyi.first_view = true;
        webview_baidufanyi.VebViewInit(url_baidu_fy, "", css_baidu);

        // Toast.makeText(getApplicationContext(), "Hello world !", Toast.LENGTH_LONG).show();
    }

    //region 三个按钮事件
    public void HandleClickTaobao(View arg0)
    {
        if (webview_taobao == null)
        {
            webview_taobao = new MyWebView(this, new Stack<MyWebView>());
            webview_taobao.first_view = true;
            webview_taobao.VebViewInit(url_taobao_so, js_taobao, css_taobao);
        }
        getSupportActionBar().hide();
        in_main = 0;
        this.webview_taobao.StartView();
        this.webview_now = this.webview_taobao;
    }
    public void HandleClickJingDong(View arg0)
    {
        if (webview_jingdong == null)
        {
            webview_jingdong = new MyWebView(this, new Stack<MyWebView>());
            webview_jingdong.first_view = true;
            webview_jingdong.VebViewInit(url_jd_so, js_jd, "");
        }
        getSupportActionBar().hide();
        in_main = 0;
        this.webview_jingdong.StartView();
        this.webview_now = this.webview_jingdong;
    }
    public void HandleClickBaiduFanyi(View arg0)
    {
        getSupportActionBar().hide();
        in_main = 0;
        this.webview_baidufanyi.StartView();
        this.webview_now = this.webview_baidufanyi;
    }
    //endregion

    public void onBackPressed()
    {
        if (webview_now != null && webview_now.canGoBack())
        {
            webview_now.goBack();
        }
        else if (in_main == 0)
        {
            setContentView(R.layout.activity_main);
            getSupportActionBar().show();
            in_main = 1;
        }
        else if (in_main == 1)
        {
            // 判断是否可后退，是则后退，否则退出程序
            if (((System.currentTimeMillis() - exitTime) > 3000))
            {
                Toast.makeText(getApplicationContext(), "再按一次返回 退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            }
            else
            {
                finish();
                System.exit(0);
            }
        }
    }

}
